package com.yanqu.road.server.cmd.specialcookboy;

import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.specialcookboy.SpecialCookBoyProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.specialcookboy.SpecialCookBoyMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import io.netty.channel.ChannelHandlerContext;

@Cmd(code = GameProtocol.S_SPECIAL_COOK_BOY_USE_SHARE_CODE_REQ_FROM_OTHER_SERVER, desc = "小当家特色服使用分享码跨服到分享码的目标服务器")
public class SpecialCookBoyCrossToServerUseCodeCmd implements Command {


    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {
        SpecialCookBoyProto.SpecialCookBoyUseShareCodeReqFromOtherServer fromOtherServer = SpecialCookBoyProto.SpecialCookBoyUseShareCodeReqFromOtherServer.parseFrom(packet.getBytes());

        long fromServerId;
        long fromUserId;
        long targetUserId;
        String shareCode;

        fromServerId = fromOtherServer.getFromServerId();
        fromUserId = fromOtherServer.getFromUserId();
        targetUserId = fromOtherServer.getTargetUserId();
        shareCode = fromOtherServer.getCode();

        SpecialCookBoyProto.SpecialCookBoyUseCodeRespGameToCross.Builder builder = SpecialCookBoyProto.SpecialCookBoyUseCodeRespGameToCross.newBuilder();
        builder.setFromServerId(fromServerId);
        builder.setFromUserId(fromUserId);
        builder.setCode(shareCode);
        //发回跨服
        UserInfo userInfo = UserMgr.getUserInfo(targetUserId);
        int ret = 0;
        if (userInfo == null){
            ret = GameErrorCode.E_SPECIAL_COOK_BOY_SHARE_CODE_ERROR;
        }else {
            ret = SpecialCookBoyMgr.useShareCode(fromUserId, targetUserId, shareCode,userInfo.getLanguage());
        }
        builder.setRet(ret);

        YanQuMessage message = YanQuMessageUtils.buildMessage(CrossProtocol.C_SPECIAL_COOK_BOY_USE_CODE_RESP_GAME_TO_CROSS, builder);
        GamePlayerMgr.sendPacket(0, message);

    }



}
